//
//  SixteenSearchViewController.swift
//  swiftTS
//
//  Created by 航电 on 2020/4/15.
//  Copyright © 2020 航电. All rights reserved.
//

import UIKit

class SixteenSearchViewController: UIViewController {

    fileprivate let cellId = "sixteen_search_cell";
    fileprivate lazy var mainTableView:UITableView = {
        let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: screenMainSize.width, height: screenMainSize.height), style: UITableView.Style.plain);
        tableView.backgroundColor = .white;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId);
        tableView.tableHeaderView = self.searBar;
        return tableView;
    }();
    
    fileprivate var dataArray:NSMutableArray = {
        var mutArr = NSMutableArray();
        
        let array = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","L","S","T","U","V","w","X","Y","Z"];
        for inde in 0...array.count-1 {
            for index in 0...5 {
                var indexString = array[inde];
                if  indexString == "H" || indexString == "M" {
                    continue;
                }
                indexString = "\(indexString)-\(index)";
                mutArr.add(indexString);
            }
        }
        
        return mutArr;
    }();
        
    fileprivate lazy var searBar:UISearchBar = {
//        let searBar = UISearchBar(frame: CGRect(x: 0, y: 88, width: screenMainSize.width, height: 50));
        let searBar = self.searchCtrl.searchBar;
        searBar.placeholder = "嘻嘻";
        searBar.barTintColor = UIColor.white;//背景颜色
        searBar.searchBarStyle = .minimal;//样式
        searBar.showsCancelButton = true;//显示cancel按钮
        searBar.delegate = self;
        searBar.tintColor = .red;//光标颜色
//        searBar.scopeButtonTitles = ["中文","英文"]; //搜索栏下的分栏按钮
        searBar.setBackgroundImage(UIImage(), for: .any, barMetrics: .default);
        return searBar;
    }();
    
    fileprivate lazy var searchCtrl:UISearchController = {
        let pushCtrl = SixteenSearchDetailViewController();
        let searchCtrl = UISearchController(searchResultsController: pushCtrl);
//        searchCtrl.isActive = false;
        return searchCtrl;
    }();
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "搜索栏使用"
        // Do any additional setup after loading the view.
        self.onCreateUIForNavigation();
        self.onCreateUIForView();
    }
        
    //TODO:UI
    fileprivate func onCreateUIForNavigation() {
//        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: UIBarButtonItem.Style.done, target: self, action: #selector(onBackAction))
    }

    fileprivate func onCreateUIForView() {
        self.view.backgroundColor = .white;
        
        self.view.addSubview(self.searBar);
        self.view.addSubview(self.mainTableView);
        
        
    }
    
    //TODO:Actions
    @objc fileprivate func onBackAction() {
        self.dismiss(animated: true, completion: nil);
    }
}

extension SixteenSearchViewController:UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count;
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId);
        cell?.backgroundColor = .lightGray;
        if self.dataArray.count > indexPath.row {
            cell?.textLabel?.text = (self.dataArray[indexPath.row] as! String);
        }
        return cell!;
    }
}

extension SixteenSearchViewController:UISearchBarDelegate {
    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
        print("开始输入:\(String(describing: searchBar.text))")
    }
    
    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
        print("结束输入:\(String(describing: searchBar.text))")
    }
    
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        print("值的改变:\(String(describing: searchBar.text))\(searchText)")
    }
    
    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        print("取消");
    }
}
